package com.quicksignin.controller;

import com.quicksignin.common.Base64;
import com.quicksignin.common.Const;
import com.quicksignin.common.Qrcode;
import com.quicksignin.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.io.File;

/**
 * Created by GHD on 2018/9/29.
 */
@Controller
@RequestMapping("/")
public class IndexController {

    @RequestMapping(value = "index")
    public String index(HttpSession session) throws Exception {
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null) {
            return "login";
        }
        return "index";
    }

    @RequestMapping(value = "createqrcode")
    public String createQrcode(HttpSession session) throws Exception{
        User teacher = (User) session.getAttribute(Const.CURRENT_USER);
        String teacherName = teacher.getTeacherName();
//        Mademd5 md5TeacherName = new Mademd5();
//        md5TeacherName.toMd5(teacherName);
        long start = System.currentTimeMillis();


        String url = "http://localhost:8080/signIn?teacherName="+ Base64.getBase64(teacherName);
        System.out.println(url);
        String img_name = teacher.getTeacherAccount();
        Qrcode.create_image(url,img_name);
        long end = System.currentTimeMillis();
        long last = end- start;
        System.out.println("二维码已生成");
        System.out.println("time consume:" + last);
        return "index";
    }

    @RequestMapping(value = "deleteqrcode")
    public String deleteQrcode(HttpSession session){
        User teacher = (User) session.getAttribute(Const.CURRENT_USER);
        String img_name = teacher.getTeacherAccount();
        File code = new File("D:\\java\\quciksignin\\target\\classes\\static\\QRcode\\"+img_name+".jpg");
        code.delete();
        System.out.println("删除成功");
        return "index";
    }

    @RequestMapping(value = "getimgname")
    @ResponseBody
    public String getImgName(HttpSession session){
        User teacher = (User) session.getAttribute(Const.CURRENT_USER);
        String img_name = teacher.getTeacherAccount();
        System.out.println("图片名字："+img_name);
        return img_name;
    }



}
